<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2020/3/3
 * Time: 15:56
 * 用户信息缓存组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Cache\Cache;

class UserCacheComponent extends Component
{

    public $_cache_config = 'users';
    public $_page_num = 30;
    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * 按属性获取用户信息
     * @param  $uid  用户id
     * @param  $tb   用户表
     * @param  $attr  用户属性
     *
     * @return 用户属性信息
     */
    public function getVal($uid, $attr = null, $tb = 'Users')
    {
        if($uid === null) {
            return null;
        }
        $key = $tb . '-' . $uid;
        $data = Cache::read($key,$this->_cache_config);
        if (empty($data)) {
            $this->createCache($uid, $tb);
            $data = Cache::read($key,$this->_cache_config);
        }

        if (empty($data)) {
            return null;
        }

        if (empty($attr)) {
            return $data;
        }

        return $data[$attr];

    }

    /**
     * 创建用户信息缓存
     * @param  $uid  用户id
     * @param  $tb   用户表
     */
    public function createCache($uid, $tb = 'Users')
    {
        $key = $tb . '-' . $uid;
        $id = $tb == 'Users' ? 'id' : 'user_id';
        $controller = $this->_registry->getController();
        $controller->loadModel($tb);
        $data = $controller->$tb->find()
            ->where([$id => $uid])
            ->first();
        Cache::write($key, $data, $this->_cache_config);
    }

    /**
     * 删除用户缓存
     */
    public function deleteUserCache($uid)
    {
        $key = 'UserInfos-' . $uid;
        Cache::delete($key, $this->_cache_config);
        $key = 'Users-' . $uid;
        Cache::delete($key, $this->_cache_config);
        $key = 'UserGroups-' . $uid;
        Cache::delete($key, $this->_cache_config);
    }

    /**
     * 删除用户列表缓存
     */
    public function deleteCache()
    {
        Cache::delete('list', $this->_cache_config);
        return true;
    }

    /**
     * 获取用户列表缓存
     */
    public function getList($data)
    {
        if(!empty($data)){
            ksort($data, SORT_NATURAL);
        }
        $key = md5(json_encode($data));
        $controller = $this->_registry->getController();
        $controller->loadModel('Users');
        $controller->loadModel('CategoryContents');
        $cache = Cache::read('list', $this->_cache_config);
        if(empty($cache[$key])){
            $category_where = [];
            $where = [];
            $category_where['content_type'] = 'users';
            if(!empty($data['category_id'])){
                $category_where['category_id'] = $data['category_id'];
                $category_content = $controller->CategoryContents->find()
                    ->select(['content_id'])
                    ->where($category_where)
                    ->order(['order_num desc'])
                    ->toArray();
                $where['id in'] = empty($category_content) ? [0] : array_column($category_content, 'content_id');
            }
            if(!empty($data['mobile'])){
                $where['mobile'] = $data['mobile'];
            }
            if(isset($data['enabled']) && in_array($data['enabled'], [0,1])){
                $where['enabled'] = $data['enabled'];
            }
            $page = empty($data['page']) ? 1 :$data['page'];
            if(!empty($data['username'])){
                $where['username like'] = '%'.$data['username'].'%';
            }
            $user = $controller->Users->find()
                ->select(['id'])
                ->where($where)
                ->limit($this->_page_num)
                ->page($page);
            $cache[$key]['total'] = $user->count();
            $user = $user->toArray();
            $cache[$key]['list'] = empty($user) ? [] : array_column($user, 'id');
            Cache::write('list', $cache, $this->_cache_config);
        }
        return $cache;
    }
}